import IPairTxData from '~/models/IData'
import IPlugin from './IPlugin'
// import ethers from 'ethers'

let addressIsContract = new Map<string, boolean>()

export default class IsContractPlugin implements IPlugin {

    name = 'IsContract'

    async modData(data: IPairTxData) {
        const swaps = data.swaps

        for (let i = 0; i < swaps.length; i++) {
            const account = swaps[i].account as string
            if (addressIsContract.get(account) === undefined) {
                swaps[i].isContract = false
                ;(window as any).em.provider.getCode(account)
                    .then((res: any) => {
                        const isContract = res !== '0x'
                        addressIsContract.set(account, isContract);
                        swaps[i].isContract = isContract
                    })
            }
            else {
                swaps[i].isContract = addressIsContract.get(account)
            }
        }  
        return data
    }
}